namespace DotNetNuke.Entities.Tabs
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Security.Permissions;
    using DotNetNuke.Services.Exceptions;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Data;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    public class TabController
    {
        public int AddTab(TabInfo objTab)
        {
            return this.AddTab(objTab, true);
        }

        public int AddTab(TabInfo objTab, bool AddAllTabsModules)
        {
            objTab.TabPath = Globals.GenerateTabPath(objTab.ParentId, objTab.TabName);
            int intTabId = DataProvider.Instance().AddTab(objTab.PortalID, objTab.TabName, objTab.IsVisible, objTab.DisableLink, objTab.ParentId, objTab.IconFile, objTab.Title, objTab.Description, objTab.KeyWords, objTab.Url, objTab.SkinSrc, objTab.ContainerSrc, objTab.TabPath, objTab.StartDate, objTab.EndDate, objTab.RefreshInterval, objTab.PageHeadText);
            TabController objTabController = new TabController();
            TabPermissionController objTabPermissionController = new TabPermissionController();
            if (objTab.TabPermissions != null)
            {
                IEnumerator refObjectHelperL0=null;
                TabPermissionCollection objTabPermissions = objTab.TabPermissions;
                TabPermissionInfo objTabPermission = new TabPermissionInfo();
                try
                {
                    refObjectHelperL0 = objTabPermissions.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        objTabPermission = (TabPermissionInfo) refObjectHelperL0.Current;
                        objTabPermission.TabID = intTabId;
                        if (objTabPermission.AllowAccess)
                        {
                            objTabPermissionController.AddTabPermission(objTabPermission);
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
            }
            if (!Null.IsNull(objTab.PortalID))
            {
                this.UpdatePortalTabOrder(objTab.PortalID, intTabId, objTab.ParentId, 0, 0, objTab.IsVisible, true);
            }
            else
            {
                ArrayList arrTabs = this.GetTabsByParentId(objTab.ParentId);
                this.UpdateTabOrder(objTab.PortalID, intTabId, (arrTabs.Count * 2) - 1, 1, objTab.ParentId);
            }
            if (AddAllTabsModules)
            {
                IEnumerator refObjectHelperL1=null;
                ModuleController objmodules = new ModuleController();
                ArrayList arrMods = objmodules.GetAllTabsModules(objTab.PortalID, true);
                try
                {
                    refObjectHelperL1 = arrMods.GetEnumerator();
                    while (refObjectHelperL1.MoveNext())
                    {
                        ModuleInfo objModule = (ModuleInfo) refObjectHelperL1.Current;
                        objmodules.CopyModule(objModule.ModuleID, objModule.TabID, intTabId, "", true);
                    }
                }
                finally
                {
                    if (refObjectHelperL1 is IDisposable)
                    {
                        (refObjectHelperL1 as IDisposable).Dispose();
                    }
                }
            }
            return intTabId;
        }

        public void CopyTab(int PortalId, int FromTabId, int ToTabId, bool IncludeContent)
        {
            ModuleController objModules = new ModuleController();
            ArrayList arrModules = objModules.GetPortalTabModules(PortalId, FromTabId);
            int refIntHelperL0 = arrModules.Count - 1;
            for (int intIndex = 0; intIndex <= refIntHelperL0; intIndex++)
            {
                ModuleInfo objModule = (ModuleInfo) arrModules[intIndex];
                if (!objModule.AllTabs)
                {
                    if (!IncludeContent)
                    {
                        objModule.ModuleID = Null.NullInteger;
                    }
                    objModule.TabID = ToTabId;
                    objModules.AddModule(objModule);
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Deletes a tab premanently from the database
        /// </summary>
        /// <param name="TabId">TabId of the tab to be deleted</param>
        /// <param name="PortalId">PortalId of the portal</param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [Vicen鏬	19/09/2004	Added skin deassignment before deleting the tab.
        /// </history>
        /// -----------------------------------------------------------------------------
        public void DeleteTab(int TabId, int PortalId)
        {
            if (this.GetTabsByParentId(TabId).Count == 0)
            {
                DataProvider.Instance().DeleteTab(TabId);
                this.UpdatePortalTabOrder(PortalId, TabId, -2, 0, 0, true, false);
            }
        }

        private TabInfo FillTabInfo(IDataReader dr)
        {
            return this.FillTabInfo(dr, true);
        }

        private TabInfo FillTabInfo(IDataReader dr, bool CheckForOpenDataReader)
        {
            TabInfo objTabInfo = new TabInfo();
            TabPermissionController objTabPermissionController = new TabPermissionController();
            bool Continue = true;
            if (CheckForOpenDataReader)
            {
                Continue = false;
                if (dr.Read())
                {
                    Continue = true;
                }
            }
            if (Continue)
            {
                try
                {
                    objTabInfo.TabID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["TabID"]), objTabInfo.TabID)));
                }
                catch (Exception exception1)
                {
                    
                    
                }
                try
                {
                    objTabInfo.TabOrder = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["TabOrder"]), objTabInfo.TabOrder)));
                }
                catch (Exception exception2)
                {
                    ProjectData.SetProjectError(exception2);
                    
                }
                try
                {
                    objTabInfo.PortalID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["PortalID"]), objTabInfo.PortalID)));
                }
                catch (Exception exception3)
                {
                    ProjectData.SetProjectError(exception3);
                    
                }
                try
                {
                    objTabInfo.TabName = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["TabName"]), objTabInfo.TabName)));
                }
                catch (Exception exception4)
                {
                    ProjectData.SetProjectError(exception4);
                    
                }
                try
                {
                    objTabInfo.IsVisible = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["IsVisible"]), objTabInfo.IsVisible)));
                }
                catch (Exception exception5)
                {
                    ProjectData.SetProjectError(exception5);
                    
                }
                try
                {
                    objTabInfo.ParentId = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["ParentId"]), objTabInfo.ParentId)));
                }
                catch (Exception exception6)
                {
                    ProjectData.SetProjectError(exception6);
                    
                }
                try
                {
                    objTabInfo.Level = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Level"]), objTabInfo.Level)));
                }
                catch (Exception exception7)
                {
                    ProjectData.SetProjectError(exception7);
                    
                }
                try
                {
                    objTabInfo.IconFile = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["IconFile"]), objTabInfo.IconFile)));
                }
                catch (Exception exception8)
                {
                    ProjectData.SetProjectError(exception8);
                    
                }
                try
                {
                    objTabInfo.DisableLink = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["DisableLink"]), objTabInfo.DisableLink)));
                }
                catch (Exception exception9)
                {
                    ProjectData.SetProjectError(exception9);
                    
                }
                try
                {
                    objTabInfo.Title = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Title"]), objTabInfo.Title)));
                }
                catch (Exception exception10)
                {
                    ProjectData.SetProjectError(exception10);
                    
                }
                try
                {
                    objTabInfo.Description = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Description"]), objTabInfo.Description)));
                }
                catch (Exception exception11)
                {
                    ProjectData.SetProjectError(exception11);
                    
                }
                try
                {
                    objTabInfo.KeyWords = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["KeyWords"]), objTabInfo.KeyWords)));
                }
                catch (Exception exception12)
                {
                    ProjectData.SetProjectError(exception12);
                    
                }
                try
                {
                    objTabInfo.IsDeleted = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["IsDeleted"]), objTabInfo.IsDeleted)));
                }
                catch (Exception exception13)
                {
                    ProjectData.SetProjectError(exception13);
                    
                }
                try
                {
                    objTabInfo.Url = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Url"]), objTabInfo.Url)));
                }
                catch (Exception exception14)
                {
                    ProjectData.SetProjectError(exception14);
                    
                }
                try
                {
                    objTabInfo.SkinSrc = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["SkinSrc"]), objTabInfo.SkinSrc)));
                }
                catch (Exception exception15)
                {
                    ProjectData.SetProjectError(exception15);
                    
                }
                try
                {
                    objTabInfo.ContainerSrc = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["ContainerSrc"]), objTabInfo.ContainerSrc)));
                }
                catch (Exception exception16)
                {
                    ProjectData.SetProjectError(exception16);
                    
                }
                try
                {
                    objTabInfo.TabPath = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["TabPath"]), objTabInfo.TabPath)));
                }
                catch (Exception exception17)
                {
                    ProjectData.SetProjectError(exception17);
                    
                }
                try
                {
                    objTabInfo.StartDate = Convert.ToDateTime(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["StartDate"]), objTabInfo.StartDate)));
                }
                catch (Exception exception18)
                {
                    ProjectData.SetProjectError(exception18);
                    
                }
                try
                {
                    objTabInfo.EndDate = Convert.ToDateTime(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["EndDate"]), objTabInfo.EndDate)));
                }
                catch (Exception exception19)
                {
                    ProjectData.SetProjectError(exception19);
                    
                }
                try
                {
                    objTabInfo.HasChildren = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["HasChildren"]), objTabInfo.HasChildren)));
                }
                catch (Exception exception20)
                {
                    ProjectData.SetProjectError(exception20);
                    
                }
                try
                {
                    objTabInfo.RefreshInterval = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["RefreshInterval"]), objTabInfo.RefreshInterval)));
                }
                catch (Exception exception21)
                {
                    ProjectData.SetProjectError(exception21);
                    
                }
                try
                {
                    objTabInfo.PageHeadText = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["PageHeadText"]), objTabInfo.PageHeadText)));
                }
                catch (Exception exception22)
                {
                    ProjectData.SetProjectError(exception22);
                    
                }
                if (objTabInfo != null)
                {
                    ArrayList arrTabPermissions = objTabPermissionController.GetTabPermissionsByPortal(objTabInfo.PortalID);
                    try
                    {
                        objTabInfo.AdministratorRoles = objTabPermissionController.GetTabPermissionsByTabID(arrTabPermissions, objTabInfo.TabID, "EDIT");
                        if (objTabInfo.AdministratorRoles == ";")
                        {
                            try
                            {
                                objTabInfo.AdministratorRoles = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["AdministratorRoles"]), objTabInfo.AdministratorRoles)));
                            }
                            catch (Exception exception23)
                            {
                                ProjectData.SetProjectError(exception23);
                                
                            }
                        }
                    }
                    catch (Exception exception24)
                    {
                        ProjectData.SetProjectError(exception24);
                        
                    }
                    try
                    {
                        objTabInfo.AuthorizedRoles = objTabPermissionController.GetTabPermissionsByTabID(arrTabPermissions, objTabInfo.TabID, "VIEW");
                        if (objTabInfo.AuthorizedRoles == ";")
                        {
                            try
                            {
                                objTabInfo.AuthorizedRoles = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["AuthorizedRoles"]), objTabInfo.AuthorizedRoles)));
                            }
                            catch (Exception exception25)
                            {
                                ProjectData.SetProjectError(exception25);
                                
                            }
                        }
                    }
                    catch (Exception exception26)
                    {
                        ProjectData.SetProjectError(exception26);
                        
                    }
                    try
                    {
                        objTabInfo.TabPermissions = objTabPermissionController.GetTabPermissionsCollectionByTabID(arrTabPermissions, objTabInfo.TabID);
                    }
                    catch (Exception exception27)
                    {
                        ProjectData.SetProjectError(exception27);
                        
                    }
                    objTabInfo.BreadCrumbs = null;
                    objTabInfo.Panes = null;
                    objTabInfo.Modules = null;
                }
                return objTabInfo;
            }
            return null;
        }

        private ArrayList FillTabInfoCollection(IDataReader dr)
        {
            ArrayList arr = new ArrayList();
            try
            {
                while (dr.Read())
                {
                    TabInfo obj = this.FillTabInfo(dr, false);
                    arr.Add(obj);
                }
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
                
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return arr;
        }

        public ArrayList GetAllTabs()
        {
            return this.FillTabInfoCollection(DataProvider.Instance().GetAllTabs());
        }

        public TabInfo GetTab(int TabId)
        {
            TabInfo GetTab;
            IDataReader dr = DataProvider.Instance().GetTab(TabId);
            try
            {
                GetTab = this.FillTabInfo(dr);
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return GetTab;
        }

        public TabInfo GetTabByName(string TabName, int PortalId)
        {
            return this.GetTabByName(TabName, PortalId, -2147483648);
        }

        public TabInfo GetTabByName(string TabName, int PortalId, int ParentId)
        {
            ArrayList arrTabs = this.FillTabInfoCollection(DataProvider.Instance().GetTabByName(TabName, PortalId));
            int intTab = -1;
            if (arrTabs != null)
            {
                switch (arrTabs.Count)
                {
                    case 0:
                        goto Label_0091;

                    case 1:
                        intTab = 0;
                        goto Label_0091;
                }
                int refIntHelperL1 = arrTabs.Count - 1;
                for (int intIndex = 0; intIndex <= refIntHelperL1; intIndex++)
                {
                    TabInfo objTab = (TabInfo) arrTabs[intIndex];
                    if (objTab.ParentId == ParentId)
                    {
                        intTab = intIndex;
                    }
                }
                if (intTab == -1)
                {
                    intTab = 0;
                }
            }
        Label_0091:
            if (intTab != -1)
            {
                return (TabInfo) arrTabs[intTab];
            }
            return null;
        }

        public ArrayList GetTabs(int PortalId)
        {
            return this.FillTabInfoCollection(DataProvider.Instance().GetTabs(PortalId));
        }

        public ArrayList GetTabsByParentId(int ParentId)
        {
            return this.FillTabInfoCollection(DataProvider.Instance().GetTabsByParentId(ParentId));
        }

        private void MoveTab(ArrayList objDesktopTabs, int intFromIndex, int intToIndex, int intNewLevel, [Optional, DefaultParameterValue(true)] bool blnAddChild)
        {
            bool blnInsert=false;
            IEnumerator refObjectHelperL0=null;
            int intOldLevel = ((TabInfo) objDesktopTabs[intFromIndex]).Level;
            if (intToIndex != (objDesktopTabs.Count - 1))
            {
                blnInsert = true;
            }
            ArrayList objClone = new ArrayList();
            int intCounter = intFromIndex;
            while (intCounter <= (objDesktopTabs.Count - 1))
            {
                if ((((TabInfo) objDesktopTabs[intCounter]).TabID == ((TabInfo) objDesktopTabs[intFromIndex]).TabID) | (((TabInfo) objDesktopTabs[intCounter]).Level > intOldLevel))
                {
                    objClone.Add(RuntimeHelpers.GetObjectValue(objDesktopTabs[intCounter]));
                    intCounter++;
                }
                else
                {
                    break;
                }
            }
            objDesktopTabs.RemoveRange(intFromIndex, objClone.Count);
            if (intToIndex > intFromIndex)
            {
                intToIndex -= objClone.Count;
            }
            if (blnInsert)
            {
                objClone.Reverse();
            }
            try
            {
                refObjectHelperL0 = objClone.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    TabInfo refObjectHelperS1;
                    TabInfo objTab = (TabInfo) refObjectHelperL0.Current;
                    objTab.TabPath = Globals.GenerateTabPath(objTab.ParentId, objTab.TabName);
                    if (blnInsert)
                    {
                        int intIncrement;
                        refObjectHelperS1 = objTab;
                        refObjectHelperS1.Level += intNewLevel - intOldLevel;
                        if (blnAddChild)
                        {
                            intIncrement = 1;
                        }
                        else
                        {
                            intIncrement = 0;
                        }
                        objDesktopTabs.Insert(intToIndex + intIncrement, objTab);
                    }
                    else
                    {
                        refObjectHelperS1 = objTab;
                        refObjectHelperS1.Level += intNewLevel - intOldLevel;
                        objDesktopTabs.Add(objTab);
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Updates child tabs TabPath field
        /// </summary>
        /// <param name="intTabid">ID of the parent tab</param>
        /// <remarks>
        /// When a ParentTab is updated this method should be called to 
        /// ensure that the TabPath of the Child Tabs is consistent with the Parent
        /// </remarks>
        /// <history>
        /// [JWhite]	16/11/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void UpdateChildTabPath(int intTabid)
        {
            IEnumerator refObjectHelperL0=null;
            ArrayList arrTabs = new TabController().GetTabsByParentId(intTabid);
            try
            {
                refObjectHelperL0 = arrTabs.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    TabInfo objtab = (TabInfo) refObjectHelperL0.Current;
                    string oldTabPath = objtab.TabPath;
                    objtab.TabPath = Globals.GenerateTabPath(objtab.ParentId, objtab.TabName);
                    if (oldTabPath != objtab.TabPath)
                    {
                        DataProvider.Instance().UpdateTab(objtab.TabID, objtab.TabName, objtab.IsVisible, objtab.DisableLink, objtab.ParentId, objtab.IconFile, objtab.Title, objtab.Description, objtab.KeyWords, objtab.IsDeleted, objtab.Url, objtab.SkinSrc, objtab.ContainerSrc, objtab.TabPath, objtab.StartDate, objtab.EndDate, objtab.RefreshInterval, objtab.PageHeadText);
                        this.UpdateChildTabPath(objtab.TabID);
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
        }

        public void UpdatePortalTabOrder(int PortalId, int TabId, int NewParentId, int Level, int Order, bool IsVisible, [Optional, DefaultParameterValue(false)] bool NewTab)
        {
            int intAddTabLevel=0;
            int intDesktopTabOrder=0;
            int intLevel=0;
            TabInfo objTab;
            bool blnValid=false;
            IEnumerator refObjectHelperL0=null;
            IEnumerator refObjectHelperL1=null;
            int intCounter = 0;
            int intFromIndex = -1;
            int intOldParentId = -2;
            int intToIndex = -1;
            int intNewParentIndex = 0;
            PortalInfo objPortal = new PortalController().GetPortal(PortalId);
            Hashtable htabs = new Hashtable();
            ArrayList objTabs = new ArrayList();
            ArrayList arrTabs = this.GetTabs(PortalId);
            try
            {
                refObjectHelperL0 = arrTabs.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    objTab = (TabInfo) refObjectHelperL0.Current;
                    if (!NewTab | (objTab.TabID != TabId))
                    {
                        TabOrderHelper refStructHelperS0 = new TabOrderHelper(objTab.TabOrder, objTab.Level, objTab.ParentId);
                        htabs.Add(objTab.TabID, refStructHelperS0);
                        if (objTab.TabOrder == 0)
                        {
                            objTab.TabOrder = 0x3e7;
                        }
                        objTabs.Add(objTab);
                        if (objTab.TabID == TabId)
                        {
                            intOldParentId = objTab.ParentId;
                            intFromIndex = intCounter;
                        }
                        if (objTab.TabID == NewParentId)
                        {
                            intNewParentIndex = intCounter;
                            intAddTabLevel = objTab.Level + 1;
                        }
                        intCounter++;
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            if (NewParentId != -2)
            {
                if (intFromIndex == -1)
                {
                    objTab = new TabInfo();
                    objTab.TabID = TabId;
                    objTab.ParentId = NewParentId;
                    objTab.IsVisible = IsVisible;
                    objTab.Level = intAddTabLevel;
                    objTabs.Add(objTab);
                    intFromIndex = objTabs.Count - 1;
                }
                if ((Level == 0) & (Order == 0))
                {
                    ((TabInfo) objTabs[intFromIndex]).IsVisible = IsVisible;
                }
            }
            if (NewParentId != -2)
            {
                if (!((intOldParentId != NewParentId) & !((intOldParentId == -2) & (NewParentId == -1))))
                {
                    if (Level == 0)
                    {
                        if (Order != 0)
                        {
                            intLevel = ((TabInfo) objTabs[intFromIndex]).Level;
                            intToIndex = -1;
                            for (intCounter = intFromIndex + Order; ((intCounter >= 0) & (intCounter <= (objTabs.Count - 1))) & (intToIndex == -1); intCounter += Order)
                            {
                                if (((TabInfo) objTabs[intCounter]).ParentId == NewParentId)
                                {
                                    intToIndex = intCounter;
                                }
                            }
                            if (intToIndex != -1)
                            {
                                if (Order == 1)
                                {
                                    intNewParentIndex = intToIndex;
                                    intToIndex = -1;
                                    for (intCounter = intNewParentIndex + 1; intCounter <= (objTabs.Count - 1); intCounter++)
                                    {
                                        if (((TabInfo) objTabs[intCounter]).Level > intLevel)
                                        {
                                            intToIndex = intCounter;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    if (intToIndex == -1)
                                    {
                                        intToIndex = intNewParentIndex;
                                    }
                                    intToIndex++;
                                }
                                this.MoveTab(objTabs, intFromIndex, intToIndex, intLevel, false);
                            }
                        }
                        goto Label_05CE;
                    }
                    intLevel = ((TabInfo) objTabs[intFromIndex]).Level;
                    blnValid = true;
                    switch (Level)
                    {
                        case -1:
                            if (intLevel == 0)
                            {
                                blnValid = false;
                            }
                            goto Label_033D;

                        case 0:
                            goto Label_033D;

                        case 1:
                            if (intFromIndex <= 0)
                            {
                                blnValid = false;
                            }
                            else if (intLevel > ((TabInfo) objTabs[intFromIndex - 1]).Level)
                            {
                                blnValid = false;
                            }
                            goto Label_033D;
                    }
                    goto Label_033D;
                }
                if (NewParentId != -1)
                {
                    intLevel = ((TabInfo) objTabs[intNewParentIndex]).Level;
                }
                else
                {
                    intLevel = -1;
                }
                intCounter = intNewParentIndex + 1;
                while (intCounter <= (objTabs.Count - 1))
                {
                    if (((TabInfo) objTabs[intCounter]).Level > intLevel)
                    {
                        intToIndex = intCounter;
                    }
                    else
                    {
                        break;
                    }
                    intCounter++;
                }
                if (intToIndex == -1)
                {
                    intToIndex = intNewParentIndex;
                }
                ((TabInfo) objTabs[intFromIndex]).ParentId = NewParentId;
                this.MoveTab(objTabs, intFromIndex, intToIndex, intLevel + 1, true);
            }
            goto Label_05CE;
        Label_033D:
            if (blnValid)
            {
                int intNewLevel;
                if (Level == -1)
                {
                    intNewLevel = intLevel + Level;
                }
                else
                {
                    intNewLevel = intLevel;
                }
                NewParentId = -2;
                for (intCounter = intFromIndex - 1; (intCounter >= 0) & (NewParentId == -2); intCounter--)
                {
                    if (((TabInfo) objTabs[intCounter]).Level == intNewLevel)
                    {
                        if (Level == -1)
                        {
                            NewParentId = ((TabInfo) objTabs[intCounter]).ParentId;
                        }
                        else
                        {
                            NewParentId = ((TabInfo) objTabs[intCounter]).TabID;
                        }
                        intNewParentIndex = intCounter;
                    }
                }
                ((TabInfo) objTabs[intFromIndex]).ParentId = NewParentId;
                if (Level != -1)
                {
                    intToIndex = intFromIndex - 1;
                    intNewLevel = intLevel + Level;
                }
                else
                {
                    intToIndex = -1;
                    for (intCounter = intNewParentIndex + 1; intCounter <= (objTabs.Count - 1); intCounter++)
                    {
                        if (((TabInfo) objTabs[intCounter]).Level > intNewLevel)
                        {
                            intToIndex = intCounter;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (intToIndex == -1)
                    {
                        intToIndex = intNewParentIndex;
                    }
                }
                if (intFromIndex == intToIndex)
                {
                    ((TabInfo) objTabs[intFromIndex]).Level = intNewLevel;
                }
                else
                {
                    this.MoveTab(objTabs, intFromIndex, intToIndex, intNewLevel, true);
                }
            }
        Label_05CE:
            intDesktopTabOrder = -1;
            int intAdminTabOrder = 0x270f;
            try
            {
                refObjectHelperL1 = objTabs.GetEnumerator();
                while (refObjectHelperL1.MoveNext())
                {
                    int intTabOrder;
                    objTab = (TabInfo) refObjectHelperL1.Current;
                    if (((((objTab.TabID == objPortal.AdminTabId) | (objTab.ParentId == objPortal.AdminTabId)) | (objTab.TabID == objPortal.SuperTabId)) | (objTab.ParentId == objPortal.SuperTabId)) & (objPortal.AdminTabId != -1))
                    {
                        intAdminTabOrder += 2;
                        intTabOrder = intAdminTabOrder;
                    }
                    else
                    {
                        intDesktopTabOrder += 2;
                        intTabOrder = intDesktopTabOrder;
                    }
                    if (htabs.Contains(objTab.TabID))
                    {
                        TabOrderHelper ttab = (TabOrderHelper) htabs[objTab.TabID];
                        if (((intTabOrder != ttab.TabOrder) | (objTab.Level != ttab.Level)) | (objTab.ParentId != ttab.ParentId))
                        {
                            this.UpdateTabOrder(objTab.PortalID, objTab.TabID, intTabOrder, objTab.Level, objTab.ParentId);
                        }
                    }
                    else
                    {
                        this.UpdateTabOrder(objTab.PortalID, objTab.TabID, intTabOrder, objTab.Level, objTab.ParentId);
                    }
                }
            }
            finally
            {
                if (refObjectHelperL1 is IDisposable)
                {
                    (refObjectHelperL1 as IDisposable).Dispose();
                }
            }
            DataCache.ClearPortalCache(PortalId, true);
        }

        public void UpdateTab(TabInfo objTab)
        {
            bool updateChildren = false;
            TabInfo objTmpTab = this.GetTab(objTab.TabID);
            if ((objTmpTab.TabName != objTab.TabName) | (objTmpTab.ParentId != objTab.ParentId))
            {
                updateChildren = true;
            }
            this.UpdatePortalTabOrder(objTab.PortalID, objTab.TabID, objTab.ParentId, 0, 0, objTab.IsVisible, false);
            DataProvider.Instance().UpdateTab(objTab.TabID, objTab.TabName, objTab.IsVisible, objTab.DisableLink, objTab.ParentId, objTab.IconFile, objTab.Title, objTab.Description, objTab.KeyWords, objTab.IsDeleted, objTab.Url, objTab.SkinSrc, objTab.ContainerSrc, objTab.TabPath, objTab.StartDate, objTab.EndDate, objTab.RefreshInterval, objTab.PageHeadText);
            TabController objTabController = new TabController();
            TabPermissionController objTabPermissionController = new TabPermissionController();
            TabPermissionCollection objTabPermissions = objTab.TabPermissions;
            if (!objTabPermissionController.GetTabPermissionsCollectionByTabID(objTab.TabID).CompareTo(objTab.TabPermissions))
            {
                IEnumerator refObjectHelperL0=null;
                objTabPermissionController.DeleteTabPermissionsByTabID(objTab.TabID);
                TabPermissionInfo objTabPermission = new TabPermissionInfo();
                try
                {
                    refObjectHelperL0 = objTabPermissions.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        objTabPermission = (TabPermissionInfo) refObjectHelperL0.Current;
                        if (objTabPermission.AllowAccess)
                        {
                            objTabPermissionController.AddTabPermission(objTabPermission);
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
            }
            if (updateChildren)
            {
                this.UpdateChildTabPath(objTab.TabID);
            }
        }

        public void UpdateTabOrder(int PortalID, int TabId, int TabOrder, int Level, int ParentId)
        {
            DataProvider.Instance().UpdateTabOrder(TabId, TabOrder, Level, ParentId);
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct TabOrderHelper
        {
            public int TabOrder;
            public int Level;
            public int ParentId;
            public TabOrderHelper(int inttaborder, int intlevel, int intparentid)
            {
                this = new TabController.TabOrderHelper();
                this.TabOrder = inttaborder;
                this.Level = intlevel;
                this.ParentId = intparentid;
            }
        }
    }
}

